package com.demo01.demo.controller;

import com.demo01.demo.Repository.OrderRepository;
import com.demo01.demo.entity.OrderTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/test")
public class OrderHandler {
    @Autowired
    private OrderRepository orderRepository;

    @GetMapping("/orderfindAll/{page}/{size}")
    public Page<OrderTable> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return orderRepository.findAll(pageable);
    }

    @GetMapping("/orderfindall")
    public List<OrderTable> findall(){
        return orderRepository.findAll();
    }

    @GetMapping("/ordersort/{page}/{size}")
    public Page<OrderTable> Sort(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Sort sort = Sort.by(Sort.Direction.ASC, "ordertime");
        Pageable pageable = PageRequest.of(page - 1, size,sort);
        return orderRepository.findAll(pageable);
    }

    @GetMapping("/ordersortall")
    public List<OrderTable> Sortall() {
        Sort sort = Sort.by(Sort.Direction.ASC, "ordertime");
        return orderRepository.findAll(sort);
    }

    @PostMapping("/ordersearch/{page}/{size}")
    public Page<OrderTable> search(@RequestBody OrderTable orderTable,@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        String[] sql = {"","","","","","","","","",""};
        if (orderTable.getUsername()!= null) {
            sql[0] = orderTable.getUsername();
        }
        if (orderTable.getDrinkid()!= null) {
            sql[1] = orderTable.getDrinkid();
        }
        if (orderTable.getDrinkname()!= null) {
            sql[2] = orderTable.getDrinkname();
        }
        if (orderTable.getDrinkinfo()!= null) {
            sql[3] = orderTable.getDrinkinfo();
        }
        if (orderTable.getDrinkprice()!= null) {
            sql[4] = orderTable.getDrinkprice().toString();
        }
        if (orderTable.getDrinknum()!= null) {
            sql[5] = orderTable.getDrinknum().toString();
        }
        if (orderTable.getOrderid()!= null) {
            sql[6] = orderTable.getOrderid();
        }
        if (orderTable.getPickid()!= null) {
            sql[7] = orderTable.getPickid();
        }
        if (orderTable.getOrdertime()!= null) {
            String[] date = orderTable.getOrdertime().split("\\.");
            sql[8] = date[0];
            sql[9] = date[1];
        }
        return orderRepository.find(sql[0],sql[1],sql[2],sql[3],sql[4],sql[5],sql[6],sql[7],sql[8],sql[9],pageable);
    }

    @PostMapping("/ordersearchall")
    public List<OrderTable> searchall(@RequestBody OrderTable orderTable) {
        String[] sql = {"","","","","","","","","",""};
        if (orderTable.getUsername()!= null) {
            sql[0] = orderTable.getUsername();
        }
        if (orderTable.getDrinkid()!= null) {
            sql[1] = orderTable.getDrinkid();
        }
        if (orderTable.getDrinkname()!= null) {
            sql[2] = orderTable.getDrinkname();
        }
        if (orderTable.getDrinkinfo()!= null) {
            sql[3] = orderTable.getDrinkinfo();
        }
        if (orderTable.getDrinkprice()!= null) {
            sql[4] = orderTable.getDrinkprice().toString();
        }
        if (orderTable.getDrinknum()!= null) {
            sql[5] = orderTable.getDrinknum().toString();
        }
        if (orderTable.getOrderid()!= null) {
            sql[6] = orderTable.getOrderid();
        }
        if (orderTable.getPickid()!= null) {
            sql[7] = orderTable.getPickid();
        }
        if (orderTable.getOrdertime()!= null) {
            String[] date = orderTable.getOrdertime().split("\\.");
            sql[8] = date[0];
            sql[9] = date[1];
        }
        return orderRepository.searchall(sql[0],sql[1],sql[2],sql[3],sql[4],sql[5],sql[6],sql[7],sql[8],sql[9]);
    }

    @GetMapping("/echarts2")
    public int[] findOrdernum(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date[]={sdf.format(new Date().getTime()-6*86400000),sdf.format(new Date().getTime()-5*86400000),sdf.format(new Date().getTime()-4*86400000),sdf.format(new Date().getTime()-3*86400000),sdf.format(new Date().getTime()-2*86400000),sdf.format(new Date().getTime()-86400000),sdf.format(new Date()),sdf.format(new Date().getTime()+86400000)};
        int data[]={orderRepository.findOrdernum(date[0],date[1]),orderRepository.findOrdernum(date[1],date[2]),orderRepository.findOrdernum(date[2],date[3]),orderRepository.findOrdernum(date[3],date[4]),orderRepository.findOrdernum(date[4],date[5]),orderRepository.findOrdernum(date[5],date[6]),orderRepository.findOrdernum(date[6],date[7])};
        return data;
    }

    @GetMapping("/allorder")
    public int countorder(){
        return orderRepository.countorder();
    }

    @GetMapping("/allsale")
    public BigDecimal allsale() {
        List<OrderTable> result = orderRepository.findAll();
        BigDecimal sum = new BigDecimal("0.00");
        for (int i = 0; i < result.size(); i++) {
            sum =sum.add(result.get(i).getDrinkprice().multiply(new BigDecimal(result.get(i).getDrinknum())));
        }
        return sum;
    }

    @GetMapping("/todaysale")
    public BigDecimal todaysale() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date[]={sdf.format(new Date()),sdf.format(new Date().getTime()+86400000)};
        List<OrderTable> result = orderRepository.findtodaysale(date[0],date[1]);
        BigDecimal sum = new BigDecimal("0.00");
        for (int i = 0; i < result.size(); i++) {
            sum =sum.add(result.get(i).getDrinkprice().multiply(new BigDecimal(result.get(i).getDrinknum())));
        }
        return sum;
    }

    @GetMapping("/echarts")
    public BigDecimal[] findsale(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date[]={sdf.format(new Date().getTime()-6*86400000),sdf.format(new Date().getTime()-5*86400000),sdf.format(new Date().getTime()-4*86400000),sdf.format(new Date().getTime()-3*86400000),sdf.format(new Date().getTime()-2*86400000),sdf.format(new Date().getTime()-86400000),sdf.format(new Date()),sdf.format(new Date().getTime()+86400000)};
        List<List<OrderTable>> result = new ArrayList<List<OrderTable>>();
        for(int i=0;i<7;i++){
            List<OrderTable> temp = orderRepository.findtodaysale(date[i],date[i+1]);
            result.add(temp);
        }
        BigDecimal sum[] = {new BigDecimal("0.00"),new BigDecimal("0.00"),new BigDecimal("0.00"),new BigDecimal("0.00"),new BigDecimal("0.00"),new BigDecimal("0.00"),new BigDecimal("0.00")};
        for (int i = 0; i < result.size(); i++) {
            for (int j = 0; j < result.get(i).size(); j++) {
                sum[i] = sum[i].add(result.get(i).get(j).getDrinkprice().multiply(new BigDecimal(result.get(i).get(j).getDrinknum())));
            }
        }
        return sum;
    }
}
